/**
 * 
 */
package com.cater.talents.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import com.cater.talents.entity.Resume;

/**
 * @Description: TODO
 * @author: yong
 * @time:2018年6月5日 下午10:33:13
 */
public class ReadWordUitl {
	public static void readAndWriterTest3() throws IOException {
		File file = new File("D:\\test\\doc\\33.doc");
		String str = "";
		try {
			FileInputStream fis = new FileInputStream(file);
			HWPFDocument doc = new HWPFDocument(fis);
			String doc1 = doc.getDocumentText();
			System.out.println(doc1);
			StringBuilder doc2 = doc.getText();
			System.out.println(doc2);
			Range rang = doc.getRange();
			String doc3 = rang.text();
			System.out.println(doc3);
			fis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String readDocx(String tfile) throws IOException {
		File file = new File(tfile);
		String str = "";
		try {
			FileInputStream fis = new FileInputStream(file);
			XWPFDocument xdoc = new XWPFDocument(fis);
			XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
			str = extractor.getText();
			System.out.println(str);
			fis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	public static void readAndWriterTest4() throws IOException {
		File file = new File("D:\\test\\doc\\22.docx");
		String str = "";
		try {
			FileInputStream fis = new FileInputStream(file);
			XWPFDocument xdoc = new XWPFDocument(fis);
			XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
			str = extractor.getText();
			Resume resume = new Resume();
			List<String> listtemp = new ArrayList<String>();
			int ik = 0;
			for (String string : str.split("\n")) {
				// System.out.println(ik + "======" + string);
				if (StringUtils.isNotEmpty(string) && !string.equals("\t")) {
					listtemp.add(string);
				}
				ik++;
			}
			for (int m = 0; m < listtemp.size(); m++) {
				System.out.println(listtemp.get(m));
				// 在职状态
				if (resume.getIschance() == null) {
					if (listtemp.get(0).indexOf("在职") >= 0) {
						resume.setIschance(1);
					} else if (listtemp.get(0).indexOf("离职") >= 0) {
						resume.setIschance(2);
					} else {
						resume.setIschance(3);
					}
				}
				if (m == 3) {
					// 姓名
					resume.setTrueName(RegexUtils.getName(listtemp.get(3)));
					// 性别
					if (listtemp.get(3).indexOf("男") >= 0) {
						resume.setSex(1);
					} else {
						resume.setSex(2);
					}
				}
				if (m == 4) {
					// 手机号码
					String phone = RegexUtils.getName(listtemp.get(4));
					for (String t_phone : phone.split("：")) {
						phone = t_phone;
					}
					if (StringUtils.isNotEmpty(phone)) {
						resume.setCellphone(phone);
						resume.setWechat(phone);
					}
				}
				if (m == 5) {
					// 邮箱
					String email = RegexUtils.getName(listtemp.get(5));
					Boolean isemail = RegexUtils.getEmail(email);
					if (isemail) {
						resume.setEmail(email);
					}
					// 教育程度
					String education = RegexUtils.getLastName(listtemp.get(5));
					resume.setEducation(education);
				}
				if (m == 6) {
					// 婚姻状况
					if (listtemp.get(6).indexOf("已婚") >= 0) {
						resume.setIschance(1);
					} else if (listtemp.get(0).indexOf("未婚") >= 0) {
						resume.setIschance(2);
					} else {
						resume.setIschance(3);
					}
				}
				if (m == 7) {
					// 所在地
					String location = RegexUtils.getName(listtemp.get(7));
				}
				if (m == 9) {
					// 公司名称
					String company = RegexUtils.getLastName(listtemp.get(9));
				}
				if (m == 10) {
					// 职位
					String position = RegexUtils.getName(listtemp.get(10));
					// 年薪
					String expectSalarystart = RegexUtils.getLastName(listtemp.get(10));
				}
				if (m == 14) {
					// 职位
					String expectedCity = RegexUtils.getName(listtemp.get(14));
				}

			}
			Pattern p = Pattern.compile("：(.*?)\\n");
			Matcher m = p.matcher(str);

			List<String> list = new ArrayList<String>();
			List<String> list2 = new ArrayList<String>();
			while (m.find()) {
				// System.out.println(m.group(1));
				list.add(m.group(1));
			}
			String phone = "";
			int i = 0;
			for (String temp : list) {
				if (temp.indexOf("在职") >= 0) {
					resume.setIschance(1);
				} else if (temp.indexOf("离职") >= 0) {
					resume.setIschance(2);
				} else {
					resume.setIschance(3);
				}
				if (i > 1) {
					System.out.println(temp);
					String regx_name = "\\t(.*?)\\t";// 符合条件的正则表达式
					Pattern pname = Pattern.compile(regx_name);
					Matcher mname = pname.matcher(temp);
					while (mname.find()) {
						list2.add(mname.group(1));
						System.out.println(mname.group(1));
					}
				}
				i++;
			}
			for (int j = 0; j < list2.size(); j++) {
				resume.setTrueName(list2.get(0));
				if (list2.get(1).indexOf("男") >= 0) {
					resume.setSex(1);
				} else {
					resume.setSex(2);
				}
				resume.setEmail(list2.get(4));
				resume.setProfession(list2.get(5));
				resume.setEducation(list2.get(6));
				for (String string : list2.get(2).split("：")) {
					phone = string;
				}
				if (StringUtils.isNotEmpty(phone)) {
					resume.setCellphone(phone);
					resume.setWechat(phone);
				}
			}
			fis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws IOException {
		// readAndWriterTest3();
		readAndWriterTest4();
	}
}
